package student.action;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.dao.*;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.DB;
import com.util.MyLog;

import edu.student.models.Announcements;
import edu.student.models.Friend;
import edu.student.models.Interest;
import edu.student.models.News;
import edu.student.models.User;

public class ViewFriend  extends ActionSupport {
    
		private static final long serialVersionUID = 1L;
		private String commandButton = "";
		private String searchBox;
		private String rollNo;
		private String name;
		private String dob;
		private boolean friends;
		private User friend;
		private Friend selection=null;
		private String username="";
		private List<News> news;
		private List<Announcements> announcements;
		private String image="";
		private Timestamp lastLogin;
		private String userid="";
		
		

        public String getUserid() {
			return userid;
		}

		public void setUserid(String userid) {
			this.userid = userid;
		}

		public List<Announcements> getAnnouncements() {
		 		return announcements;
		 	}

		 	public void setAnnouncements(List<Announcements> announcements) {
		 		this.announcements = announcements;
		 	}

		 	public List<News> getNews() {
		 		return news;
		 	}

		 	public void setNews(List<News> news) {
		 		this.news = news;
		 	}
		 	public String getUsername() {
		 		return username;
		 	}

		 	public void setUsername(String username) {
		 		this.username = username;
		 	}
		 	

		 	public String getImage() {
		 		return image;
		 	}

		 	public void setImage(String image) {
		 		this.image = image;
		 	}
		 	public Timestamp getLastLogin() {
		 		return lastLogin;
		 	}

		 	public void setLastLogin(Timestamp lastLogin) {
		 		this.lastLogin = lastLogin;
		 	}
		 public Friend getSelection() {
			return selection;
		}

		public void setSelection(Friend selection) {
			this.selection = selection;
		}

		private ArrayList<Interest> interests;
		public String getRollNo() {
			return rollNo;
		}

		public void setRollNo(String rollNo) {
			this.rollNo = rollNo;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getDob() {
			return dob;
		}

		public void setDob(String dob) {
			this.dob = dob;
		}
		

		
		public boolean isFriends() {
			return friends;
		}

		public void setFriends(boolean friends) {
			this.friends = friends;
		}

		public User getFriend() {
			return friend;
		}

		public void setFriend(User friend) {
			this.friend = friend;
		}

		public ArrayList<Interest> getInterests() {
			return interests;
		}

		public void setInterests(ArrayList<Interest> interests) {
			this.interests = interests;
		}

		public String getCommandButton() {
			return commandButton;
		}

		public void setCommandButton(String commandButton) {
			this.commandButton = commandButton;
		}
		
	    
		public String getSearchBox() {
			return searchBox;
		}

		public void setSearchBox(String searchBox) {
			this.searchBox = searchBox;
		}

		public String execute() {
			MyLog.log("in Login.execute() with commandButton = " + 
	    	        this.commandButton);
			System.out.println("In login action");
			try{
				if(searchBox!="" && commandButton.equals("search"))
				System.out.println("searchBox value is"+searchBox);
				else System.out.println("sorry searchBox value is "+searchBox);
				User user=new User();
				Map<String, Object> session = ActionContext.getContext().getSession();
				
				if(session.get("user") != null && session.get("user") != "")
					user = (User) session.get("user");
				session.put("user", user);
					rollNo = user.getRollNor();
					username=user.getUsername();
					image=user.getImage();
					lastLogin=user.getLastLogin();
					searchBox=SearchRollResult.fRollNo;
					FriendDao.insert(SearchRollResult.fRollNo, rollNo);
					addActionMessage(SearchRollResult.fRollNo+" and "+rollNo +" are friends now");
					System.out.println("RollNo "+rollNo);
					System.out.println("search value "+SearchRollResult.fRollNo);
					friend= User.findone("Select * from user where ROLL_NO ="+"'"+SearchRollResult.fRollNo+"'");
					friend.setInterests(Interest.getInterests(" where u.ROLL_NO='"+SearchRollResult.fRollNo+"' "));
					//System.out.println("interest is" + friend.getInterests());
					selection = FriendDao.findOne("where ROLL_NO = '"+rollNo+"' AND FRIEND_ROLL_NO = '"+SearchRollResult.fRollNo+"'");
					//System.out.println("friend roll no is  "+selection.getFriendRollNo());
					if(selection.getFriendRollNo()!=null)
					{System.out.println("selection is not null");
						friends = true;}
					else{System.out.println("selection is null");
					friends=false;
					}
					news = News.getNews(" where ui.roll_No='"+user.getRollNor()+"' ");
					announcements=Announcements.getAnnouncements(" where ui.roll_No='"+user.getRollNor()+"' ");
					
					System.out.println("in add function "+commandButton + SearchRollResult.fRollNo+"   "+rollNo);
									
					       
			} catch (Exception e) {
				MyLog.log("Error while checking credentials from database"+e);
			}
			return "success";

	    }
			       
	   
	}


 

